package com.design.fitment.fragment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.AbsListView;

import com.design.fitment.R;
import com.design.fitment.adapter.QuestionsanswersListAdapter;
import com.design.fitment.entity.QuestionsanswersListEntity;
import com.design.fitment.util.DesignNetworkUtil;
import com.design.fitment.util.RequestDataCallback;
import com.design.fitment.view.XListView;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;


public class FragmentNewestFragment extends Fragment implements XListView.IXListViewListener  {
    @ViewInject(R.id.xListView)
    private XListView mListView;




    private QuestionsanswersListAdapter mAdapter=null;
    private List<QuestionsanswersListEntity.DataEntity> items=new ArrayList<>();
    private Handler mHandler=null;
    private int start = 0;
    private static int refreshCnt = 0;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dynamic, null);
        ViewUtils.inject(this, view);
        return view;
    }
    public void onResume() {
        super.onResume();
        initialize();
    }

    public void initialize() {
        geneItems();

        mListView.setPullLoadEnable(true);
        mAdapter = new QuestionsanswersListAdapter(getActivity(),items);
        mListView.setAdapter(mAdapter);
//		mListView.setPullLoadEnable(false);
//		mListView.setPullRefreshEnable(false);
        mListView.setXListViewListener(this);

        mHandler = new Handler();

    }
    private void geneItems() {
        DesignNetworkUtil.getInstance().getNewest(new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                QuestionsanswersListEntity banner = gson.fromJson(response, QuestionsanswersListEntity.class);
                if (banner != null) {
                    items.addAll(banner.getData());


                    // timerChangeBanner();
                }

                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailure(HttpException error) {
                Log.d("onFailure", "onFailure() returned: " + "error");
            }
        });
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                start = ++refreshCnt;
                items.clear();
                geneItems();
                mAdapter.notifyDataSetChanged();
                mAdapter = new QuestionsanswersListAdapter(getActivity(),items);
                mListView.setAdapter(mAdapter);
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                geneItems();
                mAdapter.notifyDataSetChanged();
                onLoad();
            }
        }, 2000);
    }

}
